﻿Imports System.Data.SqlClient
Public Class FormProductEditor
    Dim dt As New DataTable
    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        dt = Cmd.ExecuteDataTable("select * from Category where Active=1")
        Me.cboCategory.DisplayMember = "CategoryName"
        Me.cboCategory.ValueMember = "CategoryId"
        Me.cboCategory.DataSource = dt

        dt = Cmd.ExecuteDataTable("select * from Type where Active=1 ")
        Me.cboType.DisplayMember = "TypeName"
        Me.cboType.ValueMember = "TypeId"
        Me.cboType.DataSource = dt
    End Sub
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        'validate other textbox...........
        If Me.txtPrice.Text = "" Then
            MsgBox("Please input product price!")
            Me.txtPrice.Focus()
            Return
        End If


        If Validator.IsDecimal(txtPrice.Text) = False Then
            MsgBox("Price must be a decimal value!")
            Me.txtPrice.Focus()
            Me.txtPrice.SelectAll()
            Exit Sub
        End If

        If Convert.ToDecimal(Me.txtPrice.Text) <= 0 Then
            MsgBox("Price must be positive")
            Me.txtPrice.Focus()
            Me.txtPrice.SelectAll()
            Exit Sub
        End If

        Cmd.Parameters("@ProductCode") = Me.txtProductCode.Text
        Cmd.Parameters("@ProductName") = Me.txtProductName.Text
        Cmd.Parameters("@CategoryId") = Me.cboCategory.SelectedValue
        Cmd.Parameters("@Price") = Me.txtPrice.Text
        Cmd.Parameters("@MinimumPrice") = Me.txtMinimumPrice.Text
        If txtId.Text = "" Then
            Cmd.ExecuteNonQuery("insert into Product values (@ProductCode,@ProductName,@CategoryId,@Price,@MinimumPrice,1)")
        Else
            Cmd.Parameters("@ProductId") = Me.txtId.Text
            Cmd.ExecuteNonQuery("update Product set ProductCode=@ProductCode,ProductName=@ProductName,CategoryId=@CategoryId,Price=@Price,MinimumPrice=@MinimumPrice where ProductId = @ProductId")
        End If

        Me.DialogResult = Windows.Forms.DialogResult.OK

    End Sub
    Private Sub FormProductEditor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI.formatForm(Me)
    End Sub

    Private Sub txtId_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtId.TextChanged

    End Sub

    Private Sub cboCategory_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCategory.Click
        'dt = Cmd.ExecuteDataTable("select * from Category where Active=1")
        'Me.cboCategory.DisplayMember = "CategoryName"
        'Me.cboCategory.ValueMember = "CategoryId"
        'Me.cboCategory.DataSource = dt

    End Sub

    Private Sub cboType_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboType.Click
        'dt = Cmd.ExecuteDataTable("select * from Type where Active=1 ")
        'Me.cboType.DisplayMember = "TypeName"
        'Me.cboType.ValueMember = "TypeId"
        'Me.cboType.DataSource = dt
    End Sub
End Class